package com.peng.leetcode.dynamic;

import java.util.Arrays;

/**
 * Rob
 * <p>
 * 213. 打家劫舍 II
 *
 * @author: lupeng6
 * @create: 2021/2/18 17:11
 */
public class Rob1 {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 1};
        System.out.println(new Rob1().rob(arr));
    }

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int[] arr1 = Arrays.copyOfRange(nums, 0, nums.length - 1);
        int[] arr2 = Arrays.copyOfRange(nums, 1, nums.length);
        return Math.max(helper(arr1), helper(arr2));
    }

    public int helper(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int size = nums.length;
        int[] dp = new int[size + 1];
        dp[0] = 0;
        dp[1] = nums[0];
        for (int i = 2; i <= size; i++) {
            dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
        }
        return dp[size];
    }
}
